package com.ttc.ssm.controller.home;

import com.ttc.ssm.common.SearchJobPage;
import com.ttc.ssm.po.*;
import com.ttc.ssm.service.admin.CommonService;
import com.ttc.ssm.service.home.HomeJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 58296 on 2016/8/17.
 */
@Controller
public class HomeSearchController {

    @Autowired
    CommonService commonService;

    @Autowired
    HomeJobService homeJobService;

    @RequestMapping("search")
    public String search (Model model) throws Exception
    {
        List<Zone> zoneList = commonService.getZone();
        List<Industry> industryList = commonService.getIndustry();
        List<JobProperty> jobPropertyList = commonService.getJobProperty();
        List<Degree> degreeList = commonService.getDegree();
        List<SalaryType> salaryTypeList = commonService.getSalaryType();

        model.addAttribute("zoneList",zoneList);
        model.addAttribute("industryList",industryList);
        model.addAttribute("jobPropertyList",jobPropertyList);
        model.addAttribute("degreeList",degreeList);
        model.addAttribute("salaryTypeList",salaryTypeList);
        return "home/index/search";
    }

    @RequestMapping("searchJob")
    public String searchJob(Model model, SearchJobPage searchJobPage) throws Exception
    {

//        System.out.println(searchJobPage);
        searchJobPage.setPageSize(9);
        searchJobPage.setCurrentPage(1);
        searchJobPage.setSearch_name(searchJobPage.getSearch_name().trim());
        int rows = homeJobService.countRowsForSearchJob(searchJobPage);
//        System.out.println(rows);
        if (rows == 0) {
            model.addAttribute("error",1);
            return "forward:/search.action";
        }
        searchJobPage.setTotalRows(rows);
        List<Job> jobList = homeJobService.selectJob(searchJobPage);
//        System.out.println(jobList);
        model.addAttribute("jobList", jobList);
        model.addAttribute("searchJobPage", searchJobPage);
        return "home/index/searchJob";
    }

    @RequestMapping("/getSearchList")
    @ResponseBody
    public Map getSearchList(@RequestParam("p") Integer p, SearchJobPage searchJobPage) throws Exception
    {
        searchJobPage.setPageSize(8);
        searchJobPage.setCurrentPage(p);
        searchJobPage.setSearch_name(searchJobPage.getSearch_name().trim());
        int rows = homeJobService.countRowsForSearchJob(searchJobPage);
        System.out.println(rows);
        searchJobPage.setTotalRows(rows);
        List<Job> jobList = homeJobService.selectJob(searchJobPage);
        Map map = new HashMap();
        map.put("data",jobList);
        map.put("show",searchJobPage);
        return map;
    }
}
